package com.cskaoyan.service.admin.profile;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketAdminExample;
import com.cskaoyan.bean.MarketNoticeAdmin;
import com.cskaoyan.bean.MarketNoticeAdminExample;
import com.cskaoyan.bean.common.BaseParam;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.admin.profile.PasswordVo;
import com.cskaoyan.bean.vo.admin.auth.Md5Util;
import com.cskaoyan.bean.vo.admin.profile.UserListVo;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketNoticeAdminMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: erdonc
 * @date: 2022/9/12 17:44
 * TODO:
 */
@Service
public class ProfileServiceImpl implements ProfileService {

    @Autowired
    MarketAdminMapper marketAdminMapper;
    @Autowired
    MarketNoticeAdminMapper marketNoticeAdminMapper;


    /**
     * @Author: erdonc
     * @date : 2022/9/12 18:55
     * TODO: 用于后台管理员密码修改
     */
    @Override
    public int updatePassword(PasswordVo passwords) {
        // 获取当前用户信息
        Subject subject = SecurityUtils.getSubject();
        MarketAdmin principal = (MarketAdmin) subject.getPrincipal();
        String username = principal.getUsername();

        // 对新旧密码进行加密
        String newPassword = null;
        String oldPassword = null;
        try {
            newPassword = Md5Util.getMd5(passwords.getNewPassword());
            oldPassword = Md5Util.getMd5(passwords.getOldPassword());
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 根据用户信息查询密码，判断输入的旧密码是否为当前管理员的旧密码
        MarketAdminExample adminExample = new MarketAdminExample();
        adminExample.createCriteria().andUsernameEqualTo(username);
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(adminExample);
        // 遍历marketAdmins，拿到旧密码
        for (MarketAdmin marketAdmin : marketAdmins) {
            String password = marketAdmin.getPassword();
            if (!password.equals(oldPassword)) {
                return -1;
            }
        }

        // 将新密码更新到数据库中
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setPassword(newPassword);
        marketAdmin.setUpdateTime(new Date());
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        criteria.andIdEqualTo(principal.getId());
        marketAdminMapper.updateByExampleSelective(marketAdmin,marketAdminExample);
        return 0;
    }


    @Override
    public UserListVo lsNotice(BaseParam page, String title, String type) {
        PageHelper.startPage(page.getPage(), page.getLimit());

        Subject subject = SecurityUtils.getSubject();
        if (subject.getPrincipals() == null) {
            subject.logout();
            return null;
        }
        MarketAdmin principal = (MarketAdmin) subject.getPrincipal();

        MarketNoticeAdminExample noticeAdminExample = new MarketNoticeAdminExample();
        MarketNoticeAdminExample.Criteria criteria = noticeAdminExample.createCriteria();
        criteria.andAdminIdEqualTo(principal.getId());

        // 无论是什么通知状态的信息，都可以做关于title的模糊查询
        if (title != null && !"".equals(title)) {
            criteria.andNoticeTitleLike("%" + title + "%");
        }

        if ("unread".equals(type)) {
            // 返回未读信息=readTime:null
            criteria.andReadTimeIsNull();
        } else if ("read".equals(type)) {
            // 返回已读信息=readtime:有时间
            criteria.andReadTimeIsNotNull();
        }

        // 返回全部信息
        List<MarketNoticeAdmin> marketNoticeAdmins = marketNoticeAdminMapper.selectByExample(noticeAdminExample);
        PageInfo<MarketNoticeAdmin> marketNoticeAdminPageInfo = new PageInfo<>(marketNoticeAdmins);
        UserListVo<MarketNoticeAdmin> marketNoticeAdminUserListVo = new UserListVo<>(marketNoticeAdminPageInfo.getTotal(), marketNoticeAdminPageInfo.getPages(),
                page.getLimit(), page.getPage(), marketNoticeAdmins);

        return marketNoticeAdminUserListVo;
    }
}
